//
//  PrivateTaxCalculator.m
//  PrivateTaxCalculator
//
//  Created by albert ma on 1/5/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "PrivateTaxCalculator.h"
#import "TaxRateDao.h"

@implementation PrivateTaxCalculator
@synthesize taxDao;

-(double)getTax:(double)income insurance:(double)insurance withType:(TaxType)type
{
    double taxIncome = income - insurance - [taxDao getTaxBase:type];
    double rate = [taxDao getTaxRate:taxIncome versionof:type];
    double deduction = [taxDao getDeducation:taxIncome versionof:type];
    double tax = 0;
    if (type == TAX_2011) 
    {
        tax = taxIncome*rate - deduction;
    }
    else
    {
        tax = taxIncome*rate - deduction;
    }
    if(tax < 0)
        tax = 0;
    return tax;
}

-(double)getTaxIncome:(double)income insurance:(double)insurance withType:(TaxType)type
{
     double taxIncome = income - insurance - [taxDao getTaxBase:type];
    if(taxIncome < 0)
        taxIncome = 0;
    return taxIncome;
}

-(double)getCurrentTax:(double )rawIncome withInsuranceBase:(double)insuranceBase withInsuracePercentage:(double)Percentage
{
    double realInsuranceBase = rawIncome > insuranceBase ? insuranceBase :rawIncome;
    double insurance = realInsuranceBase * Percentage;   
    double tax = [self getTax:rawIncome insurance:insurance withType:TAX_2011];
    return tax;
}

-(double)getCurrentTaxIncome:(double )rawIncome withInsuranceBase:(double)insuranceBase withInsuracePercentage:(double)Percentage
{
    double realInsuranceBase = rawIncome > insuranceBase ? insuranceBase :rawIncome;
    double insurance = realInsuranceBase * Percentage;   
    double taxIncome = (rawIncome - insurance - [taxDao getTaxBase:TAX_2011]);
    if(taxIncome < 0)
    {
        taxIncome = 0;
    }
    return taxIncome;
}

-(double)getInsuranceBase:(double) totalInsurance withInsuranceRate:(double)totalRate
{
    if (totalInsurance > 0 && totalRate) {
        return (totalInsurance/totalRate);
    }
    return 0;
}

-(void)dealloc
{
    [super dealloc];
}

@end
